home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0429.dms / q0429.adf / libray / libtext / textaux.c < prev    next >
C/C++ Source or Header  |  1991-08-08  |  6KB  |  294 lines

  1. /*
  2.  * textaux.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb, Rod G. Bogart, Robert F. Skinner
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: textaux.c,v 4.0 91/07/17 14:44:05 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    textaux.c,v $
  19.  * Revision 4.0  91/07/17  14:44:05  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "texture.h"
  24.  
  25. Color *
  26. ColormapRead(filename)
  27. char *filename;
  28. {
  29.     FILE *fp;
  30.     Color *map;
  31.     char buf[BUFSIZ];
  32.     int r, g, b, i, num;
  33.  
  34.     fp = fopen(filename, "r");
  35.     if (fp == (FILE *)NULL) {
  36.         RLerror(RL_ABORT, "Cannot open colormap file \"%s\".\n",
  37.                 filename);
  38.         return (Color *)NULL;
  39.     }
  40.  
  41.     map = (Color *)Calloc(256, sizeof(Color));
  42.  
  43.     for (i = 0; fgets(buf,BUFSIZ,fp) != NULL && i < 256; i++) {
  44.         num = sscanf(buf,"%d %d %d", &r, &g, &b);
  45.         if (num != 3) {
  46.             RLerror(RL_ABORT,
  47.                 "%s, line %d:  Bad color specification.\n",
  48.                 filename, i+1);
  49.             return (Color *)NULL;
  50.         }
  51.         map[i].r = (Float)r;
  52.         map[i].g = (Float)g;
  53.         map[i].b = (Float)b;
  54.         ColorScale(1. / 255., map[i], &map[i]);
  55.     }
  56.     (void)fclose(fp);
  57.     return map;
  58. }
  59.  
  60. Float
  61. Marble(vec)
  62. Vector *vec;
  63. {
  64.     Float i;
  65.  
  66.     i = sin(8. * Chaos(vec, 6) + 7. * vec->z) + 1;
  67.     
  68.     return pow(0.5 * i, 0.77);
  69. }
  70.  
  71. Float
  72. PAChaos(vec, octaves)
  73. Vector *vec;
  74. int octaves;
  75. {
  76.     Float s, t, tmp;
  77.     Vector tp;
  78.  
  79.     s = 1.0;
  80.     t = 0.;
  81.     tp = *vec;
  82.  
  83.     while (octaves--) {
  84.         tmp = Noise3(&tp) * s;
  85.         t += fabs(tmp);
  86.         s *= 0.5;
  87.         tp.x *= 2.;
  88.         tp.y *= 2.;
  89.         tp.z *= 2.;
  90.     }
  91.  
  92.     return t;
  93. }
  94.  
  95. Float
  96. Chaos(vec, octaves)
  97. Vector *vec;
  98. int octaves;
  99. {
  100.     Float s, t;
  101.     Vector tp;
  102.  
  103.     s = 1.0;
  104.     t = 0.;
  105.     tp = *vec;
  106.  
  107.     while (octaves--) {
  108.         t += Noise3(&tp) * s;
  109.         s *= 0.5;
  110.         tp.x *= 2.;
  111.         tp.y *= 2.;
  112.         tp.z *= 2.;
  113.     }
  114.  
  115.     return t;
  116. }
  117.  
  118. void
  119. VfBm(vec, omega, lambda, octaves, ans)
  120. Vector *vec, *ans;
  121. Float omega, lambda;
  122. int octaves;
  123. {
  124.     Float o;
  125.     Vector tp, n;
  126.  
  127.     ans->x = ans->y = ans->z = 0.;
  128.     tp = *vec;
  129.     o = 1.;
  130.  
  131.     while (octaves--) {
  132.         DNoise3(&tp, &n);
  133.         ans->x += o * n.x;
  134.         ans->y += o * n.y;
  135.         ans->z += o * n.z;
  136.         o *= omega;
  137.         if (o < EPSILON)
  138.             break;
  139.         tp.x *= lambda;
  140.         tp.y *= lambda;
  141.         tp.z *= lambda;
  142.     }
  143. }
  144.  
  145. Float
  146. fBm(vec, omega, lambda, octaves)
  147. register Vector *vec;
  148. Float omega, lambda;
  149. int octaves;
  150. {
  151.     Float a, o;
  152.     Vector tp;
  153.  
  154.     a = 0; o = 1.;
  155.     tp = *vec;
  156.     while (octaves--) {
  157.         a += o * Noise3(&tp);
  158.         tp.x *= lambda;
  159.         tp.y *= lambda;
  160.         tp.z *= lambda;
  161.         o *= omega;
  162.     }
  163.     return a;
  164. }
  165.  
  166. int
  167. TileValue(tileu, tilev, u, v)
  168. Float tileu, tilev, u, v;
  169. {
  170.     /*
  171.      * If both tileu and tilev are zero, the pattern is repeated infinitly.
  172.      *   XXXXXX
  173.      *   XXXXXX   tileu=0  tilev=0
  174.      *   XXXXXX
  175.      *   XXXXXX
  176.      * If one is positive and the other is zero, the pattern is infinite
  177.      * in one direction and limited in the other.
  178.      *   ++++++
  179.      *   XXXXXX   tileu=0  tilev=1
  180.      *   ++++++
  181.      *   ++++++
  182.      * If both are positive, the pattern is limited in both directions.
  183.      *   ++++++
  184.      *   +++XX+   tileu=2  tilev=1
  185.      *   ++++++
  186.      *   ++++++
  187.      * If one is negative and the other is zero, the pattern is the
  188.      * inverse of the positive/zero case.
  189.      *   XXXXXX
  190.      *   ++++++   tileu=0  tilev=-1
  191.      *   XXXXXX
  192.      *   XXXXXX
  193.      * If one is negative and the other is positive, the pattern is like
  194.      * negative/zero, but limited in one direction.
  195.      *   +++XX+
  196.      *   ++++++   tileu=2  tilev=-1
  197.      *   +++XX+
  198.      *   +++XX+
  199.      * If both are negative, the pattern is the inverse of the 
  200.      * positive/positive case (a rectangular hole).
  201.      *   XXXXXX
  202.      *   XXX++X   tileu=-2  tilev=-1
  203.      *   XXXXXX
  204.      *   XXXXXX
  205.      */
  206.     if ((tileu < 0.001) && (tileu > -0.001) && 
  207.         (tilev < 0.001) && (tilev > -0.001))
  208.         /* zero/zero */
  209.         return FALSE;
  210.     if ((tileu < -0.001) && (tilev < -0.001) &&
  211.         ((u > -tileu) || (u < 0) || (v > -tilev) || (v < 0)))
  212.         /* negative/negative */
  213.         return FALSE;
  214.     if ((tileu > 0.001) && ((u > tileu) || (u < 0)))
  215.         /* positive/whatever */
  216.         return TRUE;
  217.     if ((tilev > 0.001) && ((v > tilev) || (v < 0)))
  218.         /* whatever/positive */
  219.         return TRUE;
  220.     if ((tileu < -0.001) && (u < -tileu) && (u > 0))
  221.         /* negative/whatever */
  222.         return TRUE;
  223.     if ((tilev < -0.001) && (v < -tilev) && (v > 0))
  224.         /* whatever/negative */
  225.         return TRUE;
  226.  
  227.     return FALSE;
  228. }
  229.  
  230. void
  231. Wrinkled(pos, lambda, omega, octaves, res)
  232. Vector *pos, *res;
  233. Float lambda, omega;
  234. int octaves;
  235. {
  236.     Float s;
  237.     Vector point, tmp;
  238.  
  239.     res->x = res->y = res->z = 0.;
  240.     s = 1.;
  241.     point = *pos;
  242.     while (octaves--) {
  243.         DNoise3(&point, &tmp);
  244.         point.x *= lambda;
  245.         point.y *= lambda;
  246.         point.z *= lambda;
  247.         res->x += tmp.x * s;
  248.         res->y += tmp.y * s;
  249.         res->z += tmp.z * s;
  250.         s *= omega;
  251.     }
  252. }
  253.  
  254. void
  255. Windy(pos,windscale,chaoscale,bumpscale,octaves,tscale,hscale,offset,res)
  256. Vector *pos, *res;
  257. Float windscale, chaoscale, bumpscale, tscale, hscale, offset;
  258. int octaves;
  259. {
  260.     Vector spoint, tmp;
  261.     Float windfield, f, scalar;
  262.  
  263.     spoint = *pos;
  264.     spoint.x *= windscale;
  265.     spoint.y *= windscale;
  266.     spoint.z *= windscale;
  267.     if (chaoscale)
  268.         windfield = chaoscale * Chaos(&spoint, 7);
  269.     else
  270.         windfield = 1.;
  271.  
  272.     DNoise3(pos, &tmp);
  273.     res->x = bumpscale * tmp.x;
  274.     res->y = bumpscale * tmp.y;
  275.     res->z = bumpscale * tmp.z;
  276.  
  277.     f = 1.;
  278.     scalar = windfield;
  279.     while (octaves--) {
  280.         f *= tscale;
  281.         spoint.x = f*pos->x;
  282.         spoint.y = f*pos->y;
  283.         spoint.z = f*pos->z;
  284.         DNoise3(&spoint, &tmp);
  285.         res->x += scalar*tmp.x;
  286.         res->y += scalar*tmp.y;
  287.         res->z += scalar*tmp.z;
  288.         scalar *= hscale;
  289.     }
  290.     res->x *= windfield + offset;
  291.     res->y *= windfield + offset;
  292.     res->z *= windfield + offset;
  293. }
  294.